package com.event.usc.background;

import java.io.InputStream;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jsoup.Jsoup;


public class EventParser {
	
	public static final String imgPrefix = "https://web-app.usc.edu/ecal";
	private Calendar calendar;
	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	public List<Event> parse(InputStream stream)
	{
		List<Event> result = new LinkedList<Event> ();
		SAXBuilder builder = new SAXBuilder();
		try
		{
			Document doc = builder.build(stream);
			Element root = doc.getRootElement();
			List<Element> events = root.getChildren("event");
			for (Element event: events)
				parseSingle(event, result);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}

	private void parseSingle(Element event, List<Event> result) {
		if (event.getChild("event-building_code")==null)
			return;
		String location = event.getChildText("event-building_code").trim().toUpperCase();
		if (location.length()!=3)
			return;
		
		String title = event.getChildText("event-title").trim();
		if (title!=null)
			title = Jsoup.parse(title.trim()).text();;
		
		String summary = event.getChildText("event-summary").trim();
		if (summary!=null)
			summary = Jsoup.parse(summary.trim()).text();
		
		String description = event.getChildText("event-description");
		if (description!=null)
			description = Jsoup.parse(description.trim()).text();;
		
		String image = event.getChildText("event-image");
		if (image==null)
			image = event.getChildText("event-image_featured");
		if (image==null)
			image = event.getChildText("event-image_thumb");
		if (image!=null)
			image = imgPrefix + image.trim();
		
		String category = getCat(event);
		long start = getStart(event);
		long end = getEnd(event);
		
		Event newevent = new Event();
		newevent.setEventTitle(title);
		newevent.setEventSum(summary);
		newevent.setEventStart(start);
		newevent.setEventEnd(end);
		newevent.setEventLoc(location);
		newevent.setEventImg(image);
		newevent.setEventDes(description);
		newevent.setEventCat(category);
		result.add(newevent);
	}

	private long getEnd(Element event) {
		Element schedule = event.getChild("event-schedule");
		String end = schedule.getChildText("event-end_time");
		return getMsec(end);
	}

	private long getMsec(String end) {	
		String tokens[] = end.split(":");
		calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tokens[0]));
		calendar.set(Calendar.MINUTE, Integer.parseInt(tokens[1]));
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}

	private long getStart(Element event) {
		Element schedule = event.getChild("event-schedule");
		String start = schedule.getChildText("event-start_time");
		return getMsec(start);
	}

	private String getCat(Element event) {
		Element cats = event.getChild("event-categories");
		if (cats==null)
			return "OTHER";
		String cat = cats.getChildText("event-category");
		if (cat!=null)
		{
			cat = cat.trim();
			if (cat.equals(""))
				cat = "OTHER";
			cat = cat.toUpperCase();
			if (cat.indexOf("ACADEMIC")!=-1)
				cat = "ACADEMIC";
			else if (cat.indexOf("ART")!=-1)
				cat = "ART";
			else if (cat.indexOf("EXHIBIT")!=-1)
				cat = "EXHIBIT";
			else if (cat.indexOf("FILM")!=-1 || cat.indexOf("THEATER")!=-1)
				cat = "FILM";
			else if (cat.indexOf("MUSIC")!=-1)
				cat = "MUSIC";
			else if (cat.indexOf("SPORT")!=-1)
				cat = "SPORTS";
			else if (cat.startsWith("W") || cat.startsWith("V") || cat.indexOf("LECTURE")!=-1)
				cat = "WORKSHOP";
			else
				cat = "OTHER";
			return cat;
		}
		else 
			return "OTHER";	
	}
	
}
